package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.MoneyFormatUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.OffWorkSumAmountEntity;
import cn.mumzone.pos.model.OffWorkSumAmountResult;
import cn.mumzone.pos.model.OffWorkTicketEntity;
import cn.mumzone.pos.presenter.OffWorkPresenter;
import cn.mumzone.pos.presenter.contract.OffWorkContract;
import cn.mumzone.pos.ui.widgets.keyboard.PosKeyBoardView;
import cn.mumzone.pos.util.print.PrinterService;

/**
 * Created by qiuzhenhuan on 2019/7/6.
 */

public class ACT_OffWork extends BaseActivity<OffWorkPresenter> implements OffWorkContract.View {


    @BindView(R.id.tv_wxpay)
    TextView tvWxPay;
    @BindView(R.id.tv_alipay)
    TextView tvAliPay;
    @BindView(R.id.tv_balance)
    TextView tvBalancePay;
    @BindView(R.id.tv_time_card)
    TextView tvCardPay;
    @BindView(R.id.tv_bank)
    TextView tvBankPay;
    @BindView(R.id.tv_integral_title)
    TextView tvIntegralTitle;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;

    @BindView(R.id.et_cash)
    EditText etCash;
    @BindView(R.id.keyboard_view)
    PosKeyBoardView keyBoardView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(cn.mumzone.basic.R.anim.right_in, cn.mumzone.basic.R.anim.right_out);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.RIGHT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        getWindow().setBackgroundDrawableResource(R.color.bg_color);
    }

    @Override
    protected void initEventAndData() {

        keyBoardView.setOnConfirmClickListener(new PosKeyBoardView.OnConfirmClickListener() {
            @Override
            public void onConfirmClick(String text) {
                if (IsFastClickUtil.isFastClick(etCash)) {
                    return;
                }
                String cash = MoneyFormatUtil.getFormattedMoney(mContext, etCash, "输入金额格式错误，请检查", true);
                if (!StringUtils.isEmpty(cash)) {
                    mPresenter.doShiftExchange(cash);
                }
            }
        });

        //请求当前员工工作时间总金额
        mPresenter.loadHandOverInfo();

        PrinterService.openDrawer(mContext);
    }


    @Override
    public void showHandOverInfo(OffWorkSumAmountResult result) {
        if (!ListUtil.isListEmpty(result.getList())) {
            //支付方式 0-微信 1-支付宝 2-银行卡     3-会员余额 4-计次卡刷卡
            for (int i = 0; i < result.getList().size(); i++) {
                OffWorkSumAmountEntity entity = result.getList().get(i);
                String moneyStr = String.format("￥ %s", StringUtils.getFormattedMoney(entity.getSum()));
                switch (entity.getPayType()) {
                    case "0":
                        tvWxPay.setText(moneyStr);
                        break;
                    case "1":
                        tvAliPay.setText(moneyStr);
                        break;
                    case "2":
                        tvBankPay.setText(moneyStr);
                    case "4":
                        tvBalancePay.setText(moneyStr);
                    case "5":
                        tvCardPay.setText(moneyStr);
                        break;
                    case "7":
                        tvIntegral.setText(moneyStr);
                }
            }
        }
        tvIntegralTitle.setText(String.format("%s积分抵现", StringUtils.getWholeNumber(BigDecimalUtil.getNumber(result.getIntegralMoney()))));
    }

    @Override
    public void showShiftExchangeSuccess() {
        PosToast.INSTANCE.makeSuccessToast(mContext, "退出成功！");

        //交班成功后，去员工登录界
        Intent intent = new Intent(mContext, ACT_Login.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();

    }

    @Override
    public void showShiftDetail(OffWorkTicketEntity entity) {
        PrinterService.doPrintOffWordTicket(mContext, entity);
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @OnClick({R.id.iv_back, R.id.iv_close})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ACT_Setting.startAc(mContext);
                finish();
                break;
            case R.id.iv_close:
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ACT_Setting.startAc(mContext);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_off_work;
    }

    public static void startAc(Context context) {
        Intent intent = new Intent(context, ACT_OffWork.class);
        context.startActivity(intent);
    }
}
